package cn.springcloud.alibaba.gateway.config;

import cn.springcloud.alibaba.gateway.handler.CaptchaHandler;
import cn.springcloud.alibaba.gateway.handler.FallbackHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.RouterFunctions;

@Slf4j
@Configuration
public class RouterConfig {

	@Autowired
	private CaptchaHandler captchaHandler;
	@Autowired
	private FallbackHandler fallbackHandler;

	@Bean
	public RouterFunction<?> routerFunction() {
		return RouterFunctions.route(
			RequestPredicates.path("/fallback")
				.and(RequestPredicates.accept(MediaType.APPLICATION_JSON_UTF8)), fallbackHandler)

			.andRoute(RequestPredicates.GET("/captcha")
				.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), captchaHandler);
	}
}
